package com.wyits;

import java.util.Scanner;

public class SqlTool {

    public static String modYearSql  = "PARTITION #TABLENAME#_#PARTFLAG##YEAR# VALUES LESS THAN (TO_DATE ('#YEAR#-#MONTH#-01', 'yyyy-mm-dd'))";
    public static String modMonthSql = "PARTITION #TABLENAME#_#PARTFLAG##YEAR##MONTH# VALUES LESS THAN (TO_DATE ('#YEAR#-#MONTH#-01', 'yyyy-mm-dd'))";
    public static String tableFlag = "#TABLENAME#";
    public static String yearFlag = "#YEAR#";
    public static String monthFlag = "#MONTH#";
    public static String partFlag = "#PARTFLAG#";
    public static String inputDayFlag = "D";
    public static String inputMonthFlag = "M";
    public static String inputYearFlag = "Y";

    public static String getMonth(int month) {
        String monthStr = String.valueOf(month);
        if (monthStr.length() == 1) {
            monthStr = "0" + monthStr;
        }
        return monthStr;
    }

    public static String getChangeSql(String tableName, int year, int month, String inputPartFlag) {
        String changeSql =null;
        if (inputMonthFlag.equals(inputPartFlag)) {
            changeSql = modMonthSql;
        }
        if (inputYearFlag.equals(inputPartFlag)) {
            changeSql = modYearSql;
        }
        changeSql = changeSql.replaceAll(tableFlag,tableName);
        changeSql = changeSql.replaceAll(yearFlag,String.valueOf(year));
        changeSql = changeSql.replaceAll(monthFlag,getMonth(month));
        changeSql = changeSql.replaceAll(partFlag,inputPartFlag);
        return changeSql;
    }

    public static void printMonthSql(String tableName, String inputPartFlag) {
        for (int month = 9; month <= 12; month++) {
            if (month == 12) {
                System.out.println(getChangeSql(tableName,2019,month,inputPartFlag));
            } else {
                System.out.println(getChangeSql(tableName,2019,month,inputPartFlag)+",");
            }
        }
        for (int year = 2022; year <= 2027; year++) {
            for (int month = 1; month <= 12; month++) {
                if (month == 12) {
                    System.out.println(getChangeSql(tableName,year,month,inputPartFlag));
                } else {
                    System.out.println(getChangeSql(tableName,year,month,inputPartFlag)+",");
                }
            }
        }
    }
    public static void printYearSql(String tableName, String inputPartFlag) {
        for (int year = 2022; year <= 2027; year++) {
            if (year == 2027) {
                System.out.println(getChangeSql(tableName,year,1,inputPartFlag));
            } else {
                System.out.println(getChangeSql(tableName,year,1,inputPartFlag)+",");
            }
        }
    }
    /**
     * ALTER TABLE #TABLENAME# ADD PARTITION #TABLENAME#_M202109 VALUES LESS THAN (TO_DATE ('2021-09-01', 'yyyy-mm-dd'));
     * @param args
     */
    public static void main(String[] args) {
        /*System.out.println("please input tableName:");
        Scanner scanner = new Scanner(System.in);
        String tableName = scanner.next();*/
        String tableName = args[0];
        String inputFlag = args[1];
        String partColName = args[2];
        System.out.println("PARTITION BY RANGE ("+partColName+")");
        System.out.println("(");
        if (inputMonthFlag.equals(inputFlag)) {
            printMonthSql(tableName,inputMonthFlag);
        }
        if (inputYearFlag.equals(inputFlag)) {
            printYearSql(tableName,inputYearFlag);
        }
        System.out.println(");");
    }
}
